<?php

    class inPlaceRichEditor extends sfRichTextEditor
    {
        protected $callback = '';
        
        public function initialize($name, $content, $options = array(), $callback = null )
        {
            parent::initialize( $name, $content, $options );
            $this->callback = $callback;
        }
        
        public function toHTML()
        {
            $i18n = _get_option( $this->options, 'i18n', sfContext::getInstance()->getI18n() );
            
            $html = "";
            $html .= "<div style='position:relative'>";
            $html .= "<span class='icon-edit' onClick='InPlace.editField( this )'; title='".$i18n->__('click here to edit contents')."' ></span>";
            $html .= "<div style='clear:both' class='editable_content'>";
            $html .= $this->content;
            $html .= "</div></div>";
            
            $callback = $this->callback;
            
            if ( $callback )
                $callback = ", ".$callback;
            $html .= "<div style='display:none; position:relative'' >";
            $html .= "<span class='icon-save' title='".$i18n->__('save')."' type='button' onclick = 'InPlace.saveField( this ".$callback." )' ></span>";
            $html .= "<span class='icon-cancel' title='".$i18n->__('cancel')."' type='button' onClick = 'InPlace.cancelField( this )' ></span>";
            $html .= "<span style='clear:both'></span>";
    
            $options = $this->options;
        
            // we need to know the id for things the rich text editor
            // in advance of building the tag
            $id = _get_option($options, 'id', get_id_from_name($this->name, null));
        
            // use tinymce's gzipped js?
            $tinymce_file = _get_option($options, 'tinymce_gzip') ? '/tiny_mce_gzip.php' : '/tiny_mce.js';
        
            // tinymce installed?
            $js_path = sfConfig::get('sf_rich_text_js_dir') ? '/'.sfConfig::get('sf_rich_text_js_dir').$tinymce_file : '/sf/tinymce/js'.$tinymce_file;
            
            if (!is_readable(sfConfig::get('sf_web_dir').$js_path))
            {
              throw new sfConfigurationException('You must install TinyMCE to use this helper (see rich_text_js_dir settings).');
            }

            sfContext::getInstance()->getResponse()->addJavascript($js_path);
            sfContext::getInstance()->getResponse()->addJavascript( '/js/tiny_mce_AJAX.js' );  
        
            require_once sfConfig::get('sf_lib_dir').DIRECTORY_SEPARATOR.'symfony'.DIRECTORY_SEPARATOR.'Helper'.DIRECTORY_SEPARATOR.'JavascriptBaseHelper.php';
        
            $tinymce_options = '';
            $style_selector  = '';
        
            // custom CSS file?
            if ($css_file = _get_option($options, 'css'))
            {
              $css_path = stylesheet_path($css_file);
        
              sfContext::getInstance()->getResponse()->addStylesheet($css_path);
        
              $css    = file_get_contents(sfConfig::get('sf_web_dir').DIRECTORY_SEPARATOR.$css_path);
              $styles = array();
              preg_match_all('#^/\*\s*user:\s*(.+?)\s*\*/\s*\015?\012\s*\.([^\s]+)#Smi', $css, $matches, PREG_SET_ORDER);
              foreach ($matches as $match)
              {
                $styles[] = $match[1].'='.$match[2];
              }
        
              $tinymce_options .= '  content_css: "'.$css_path.'",'."\n";
              $tinymce_options .= '  theme_advanced_styles: "'.implode(';', $styles).'"'."\n";
              $style_selector   = 'styleselect,separator,';
            }
            
            $culture = _get_option($options, 'culture');
            if( !$culture )
                sfContext::getInstance()->getUser()->getCulture();
        
            $tinymce_js = '
        tinyMCE.init({
          mode: "exact",
          language: "'.strtolower(substr($culture, 0, 2)).'",
          elements: "'.$id.'",
          plugins: "table,advimage,advlink,flash,template",
          theme: "advanced",
          theme_advanced_toolbar_location: "top",
          theme_advanced_toolbar_align: "left",
          theme_advanced_path_location: "none",
          theme_advanced_buttons1: "template,separator,bold,italic,strikethrough,separator,sub,sup,separator,charmap",
          theme_advanced_buttons2: "bullist,numlist,separator,outdent,indent,separator,undo,redo,separator,link,unlink,image,flash,separator,cleanup,removeformat",
          theme_advanced_buttons3: "",
          extended_valid_elements: "img[class|src|border=0|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name|style]",
          relative_urls: true,
          debug: false
          '.($tinymce_options ? ','.$tinymce_options : '').'
          '.(isset($options['tinymce_options']) ? ','.$options['tinymce_options'] : '').'
        });';
        
            if (isset($options['tinymce_options']))
            {
              unset($options['tinymce_options']);
            }
            
            $html .= content_tag('script', javascript_cdata_section($tinymce_js), array('type' => 'text/javascript'));
            $html .= content_tag('textarea', $this->content, array_merge(array('name' => $this->name, 'id' => $id), _convert_options($options)));
            $html .= "</div>";
            
            return $html;
        }
    }


?>